﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WXshopBuiness;
using Services.WXshopBuiness;
namespace WeiXinpProAdmin.Controllers
{
    public class PromotionsController : AdminBaseController
    {
        public PromotionServices m_PromotionServices;
        public WXProductServices m_WXProductServices;
        public PromotionsController(PromotionServices mPromotionServices, WXProductServices mWXProductServices)
        {
            m_WXProductServices = mWXProductServices;
           m_PromotionServices =   mPromotionServices;
        }

        // GET: Promotions
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ProductPromotionEdit(string productId)
        {
            ViewData["Promotions"] = m_PromotionServices.GetAllPromotions();
            ViewBag.PromotionsId  = null;
            WXProduct model = null;
            if (!string.IsNullOrEmpty(productId))
            {
                WXProduct product = m_WXProductServices.GetById(productId);
                if (product != null)
                {
                    
                    model = product;
                    if (!string.IsNullOrEmpty(product.PromotionId))
                    {
                        ViewBag.PromotionsId = product.PromotionId;
                    }
                }
            }
            return View(model);
        }

        public ActionResult PromotionList()
        {
            return View();
        }


        public JsonResult GePromotionList(int offset, int limit, string promotionName, DateTime? startTime, DateTime? endTime,int? Disable, string PromotionUnidName)
        {
            var lstRes = new List<Promotions>();
            int total = 0;
            lstRes = m_PromotionServices.GetPromotionList(offset, limit, promotionName,startTime, endTime, PromotionUnidName,Disable,  out total);
            return Json(new { total = total, rows = lstRes }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult PromotionEdit(string promotionId)
        {
            Promotions model = null;
            if (!string.IsNullOrEmpty(promotionId))
            {
                model = m_PromotionServices.GetPromotionsById(promotionId);
            }
            ViewBag.promotionId = promotionId;
            return View(model);
        }


        public JsonResult EditProductPromotions(FormCollection collection)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
            string Id = collection["ProductID"];
            WXProduct product = new WXProduct();
            if (!string.IsNullOrEmpty(Id))
            {
                  product=  m_WXProductServices.GetById(Id);
            }
            product.PromotionId = collection["promotionId"];
           success=  m_WXProductServices.UpdateWXProduct(product);
            result = success ? "更新产品活动配置成功" : "更新产品活动配置失败";
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult EditPromotion(FormCollection collection)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
            string Id = collection["PromotionId"];
            Promotions promotion = new Promotions();
            if (!string.IsNullOrEmpty(Id))
            {
                promotion = m_PromotionServices.GetPromotionsById(Id);
            }
            promotion.CreateTime =DateTime.Now;     
            string Disable = collection["Disable"];
            promotion.Disable = int.Parse(string.IsNullOrEmpty(Disable) ? "0" : ((Disable.ToString() == "on") ? "1" : "0"));
            DateTime startTime = DateTime.Now;
            DateTime endtime = DateTime.Now.AddDays(7);            
            DateTime.TryParse( collection["StartTime"],out startTime);
            DateTime.TryParse(collection["EndTime"], out endtime);
            promotion.StartTime = startTime;
            promotion.EndTime = endtime;
            promotion.Param1 = collection["Param1"];
            promotion.Param2 = collection["Param2"];
            promotion.Param3 = collection["Param3"];
            promotion.Param4 = collection["Param4"];
            promotion.Param5 = collection["Param5"];
            promotion.Param1Name = collection["Param1Name"];
            promotion.Param2Name = collection["Param2Name"];
            promotion.Param3Name = collection["Param3Name"];
            promotion.Param4Name = collection["Param4Name"];
            promotion.Param5Name = collection["Param5Name"];
            promotion.PromotionName = collection["PromotionName"];
            promotion.Remarks = collection["Remarks"];
            if (!string.IsNullOrEmpty(Id))
            {
             success =   m_PromotionServices.EditPromotions(promotion);
            }
            else //新增
            {
                promotion.PromotionID = Guid.NewGuid().ToString();
                promotion.PromotionUnidName = collection["PromotionUnidName"];
                success = m_PromotionServices.AddPromotions(promotion);
            }                           
            result = success ? "编辑成功" : "编辑失败";
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DelPromotion(string promotionId)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
            if (!string.IsNullOrEmpty(promotionId))
            {
                Promotions promotions = m_PromotionServices.GetPromotionsById(promotionId);
                success = m_PromotionServices.DelPromotions(promotions);
                result = success ? "删除成功" : "删除失败";
            }
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
    }
}